package com.vne.skyemart.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.vne.skyemart.model.Products;
import com.vne.skyemart.service.ProductsService;

@Controller
@SessionAttributes
@RequestMapping("/products")
public class ProductsController {

	@Autowired
	private ProductsService productsService;

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ModelAndView saveProducts(@ModelAttribute(" products") Products products,
			BindingResult result) {
		 productsService.addProducts(products);
		return new ModelAndView("redirect:/products.html");
	}

	@RequestMapping(method = RequestMethod.GET)
	public String listProducts(Model model) {
		model.addAttribute("products",  productsService.listProducts());

		return "productsList";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public ModelAndView addProducts(@ModelAttribute("products") Products products,
			BindingResult result) {
		return new ModelAndView("addProducts");
	}

}